package com.orchid.community.models.controller;

import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

//@CrossOrigin //跨域
@RestController
@RequestMapping("/obs")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public MsgResponse upload(@RequestParam("files") MultipartFile files){

        try {
            InputStream inputStream = files.getInputStream();
            String originalFilename = files.getOriginalFilename();
            String uploadUrl = fileService.upload(inputStream, originalFilename);

            //返回r对象
            return ResultUtils.success(uploadUrl);

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }
}
